
package cz.cacek.ebook.packager;

import java.io.File;

import cz.cacek.ebook.BookInfo;
import cz.cacek.ebook.Utils;

/**
 * Book representation
 * @author Tom� Darmovzal [tomas.darmovzal (at) seznam.cz]
 * @author Josef Cacek [josef.cacek (at) atlas.cz]
 * @author $Author: kwart $
 * @version $Revision: 1.13 $
 * @created $Date: 2007/08/31 13:49:49 $
 */
public class Book extends BookInfo {

	/**
	 *
	 */
	private static final long serialVersionUID = -686939415874490167L;


	/**
	 * Minimum of characters in one part of book
	 */
	public static final int MIN_PART_SIZE = 200;

	private String description;
	private String text;
	private int partSize;
	private boolean rightToLeft = false;

	private String filepath;//文件路径


	//protected final static ResourceProvider res = ResourceProvider.getInstance();

	// GUI can use this
	public Book() {
		setName(Constants.BOOK_NAME_DEF);
		setDescription("");
		setText("");
		setPartSize(Constants.PROP_PARTSIZE_DEF);
	}

	/**
	 * Creates Book object from given parameters
	 * @param aName name of book
	 * @param aDesc description
	 * @param aPartSize size of parts to which will be book divided
	 * (and size of read buffer in ebook application)
	 */
	public Book(String aName, String aDesc, int aPartSize) {
		setName(aName);
		setDescription(aDesc);
		setPartSize(aPartSize);
	}

	/**
	 * @param aFile filename of plain-text file from which will be book content generated
	 * @param aCharset
	 */
	public void readText(String aFile, String aCharset) {
		if(Utils.INFO){
			Utils.info("path:"+aFile+"\t encoding:"+aCharset);
		}

		String tmpText = null;
		if (aFile != null) {

			File tmpFile = new File(aFile);
			try {
				tmpText = ApertureExtractor.parseText(tmpFile, StringUtils.getKnownCharset(aCharset), this);
			} catch (Throwable ex) {
				//something's wrong, try to read simple text
			}
			if (tmpText==null) {
				try {
					tmpText = StringUtils.read(tmpFile, StringUtils.getKnownCharset(aCharset));
				} catch (Throwable ex2) {
					//no way
				}
			}
		}
		setText(tmpText);
	}

	/**
	 * Returns ID generated from name
	 * @return bookID
	 */
	public String getId() {
		String id = super.getId();
		if (id == null) {
			id = Utils.createIdFromName(this.getName());
		}
		this.setId(id);
		return id;
	}

	/**
	 * Returns description of book
	 * @return book description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets description
	 * @param aDesc
	 */
	public void setDescription(String aDesc) {
		description = aDesc==null?"":aDesc;
	}



	/**
	 * Returns book name without special chars. Because of Manifest and Jad file.
	 * @return book name without special chars
	 */
	public String getAsciiName() {
		return this.getName().replaceAll("[^a-zA-Z \t\n]", "_");
	}

	/**
	 * Sets book name
	 * @param aName
	 * @throws PackagerRuntimeException name is empty
	 */
	public void setName(String aName) {
		if (aName == null || aName.trim().length()==0) {
			aName = Constants.BOOK_NAME_DEF;
		} else {
			aName = aName.trim();
		}
		super.setId(null);
		super.setName(aName);
	}

	/**
	 * Returns size of parts
	 * @return size of one book part
	 */
	public int getPartSize() {
		return partSize;
	}

	/**
	 * Sets size of parts
	 * @param aPart
	 * @throws PackagerRuntimeException size is &lt; 1
	 */
	public void setPartSize(int aPart) {
		if (aPart<MIN_PART_SIZE) {
			aPart = Constants.PROP_PARTSIZE_DEF;
		}
		partSize = aPart;
	}

	/**
	 * @return Returns the text.
	 */
	public String getText() {
		if (text==null || text.length()==0) {
			text = "...";
		}
		return text;
	}

	/**
	 * Sets text of book without any formating.
	 * @param aText
	 */
	void setTextWithoutChange(String aText) {
		text = aText;
	}

	/**
	 * Sets text of book and formats it better readability on mobile devices (only if enabled in
	 * properties).
	 * @param aText The text to set.
	 */
	public void setText(String aText) {
		if (aText == null) {
			aText = "";
		} else {
			//Edit input text to better readability on mobile devices
			if (PropertyProvider.getInstance().getAsBool(Constants.PROP_AUTOFORMAT)) {
				//replace line ends and tabs
				aText = aText.replaceAll("\r","").replaceAll("\t", " ");
				//trim spaces
				aText = aText.replaceAll("[ ]*\n[ ]*","\n").replaceAll("[ ]+"," ");
				//replace multiple lineends
				aText = aText.replaceAll("\n[\n]+","\n\n");
			}
		}
		this.text = aText;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "" + getName();
	}

	public boolean isRightToLeft() {
		return rightToLeft;
	}

	public void setRightToLeft(boolean rightToLeft) {
		this.rightToLeft = rightToLeft;
	}

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}


}
